package VIEWS;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import CONTROLLERS.PhongKhamControllers;
import ModelEntityFW.KhoaEntity;
import ModelEntityFW.PhongKhamEntity;
import ModelEntityFW.UserEntitys;
import javax.swing.JToolBar;
import java.awt.Toolkit;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.DefaultComboBoxModel;

public class frmEditPhongKham extends JFrame {

	private JPanel contentPane;
	private JTextField maPK;
	private JTextField tenPK;
	private static Connection m_objConnections;
	private static UserEntitys m_objSecurityContext;
	private static PhongKhamEntity m_objPhongKhamEntity;
	private PhongKhamControllers m_objControllers;
	private JComboBox tenKhoa;
	private JComboBox oHour;
	private JComboBox oMinute;
	private JComboBox cHour;
	private JComboBox cMinute;

	/**
	 * Launch the application.
	 */
	/*
	 * public static void main(String[] args) { EventQueue.invokeLater(new
	 * Runnable() { public void run() { try { frmEditPhongKham frame = new
	 * frmEditPhongKham(); frame.setVisible(true); } catch (Exception e) {
	 * e.printStackTrace(); } } }); }
	 */

	/**
	 * Create the frame.
	 */
	public frmEditPhongKham(Connection objConnection, UserEntitys objSecurity, PhongKhamEntity objPhongKhamEntity) {
		m_objConnections = objConnection;
		m_objSecurityContext = objSecurity;
		m_objPhongKhamEntity = objPhongKhamEntity;
		m_objControllers = new PhongKhamControllers(m_objConnections, m_objSecurityContext);

		setIconImage(Toolkit.getDefaultToolkit().getImage(frmEditPhongKham.class.getResource("/images/13.png")));
		setTitle("Chỉnh sửa thông tin phòng khám");
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 441, 356);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JToolBar toolBar = new JToolBar();
		toolBar.setBounds(0, 0, 435, 43);
		contentPane.add(toolBar);

		JButton btnNewButton = new JButton();
		
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				m_objPhongKhamEntity.setTenPhongKham(tenPK.getText());
				m_objPhongKhamEntity.setOpenTime(oHour.getSelectedItem() + ":" + oMinute.getSelectedItem() + ":00");
				m_objPhongKhamEntity.setCloseTime(cHour.getSelectedItem() + ":" + cMinute.getSelectedItem() + ":00");
				m_objPhongKhamEntity.setMaKhoa((tenKhoa.getSelectedIndex() + 2));
				if (m_objPhongKhamEntity.isbFlag()) {
					boolean bResult = false;
					try {
						bResult = m_objControllers.ExecOfUpdateElement(m_objPhongKhamEntity);
					} catch (Exception e) {
						// TODO: handle exception
						JOptionPane.showMessageDialog(getParent(), "Lỗi: " + e.getMessage(), "ERROR!!!",
								JOptionPane.ERROR_MESSAGE);
					}

					if (bResult) {
						JOptionPane.showMessageDialog(getParent(), "Cập nhật thành công!!!", "Thông báo",
								JOptionPane.INFORMATION_MESSAGE);
					}
				}else{
					boolean bResult = false;
					try {
						bResult = m_objControllers.ExecOfInsertElement(m_objPhongKhamEntity);
					} catch (Exception e) {
						// TODO: handle exception
						JOptionPane.showMessageDialog(getParent(), "Lỗi: " + e.getMessage(), "ERROR!!!",
								JOptionPane.ERROR_MESSAGE);
					}

					if (bResult) {
						JOptionPane.showMessageDialog(getParent(), "Thêm mới thành công!!!", "Thông báo",
								JOptionPane.INFORMATION_MESSAGE);
					}
				}
				dispose();
			}
		});
		btnNewButton.setIcon(new ImageIcon(frmEditPhongKham.class.getResource("/images/3_2.png")));
		btnNewButton.setFont(new Font("Cambria", Font.PLAIN, 14));
		toolBar.add(btnNewButton);

		JButton btnng = new JButton("Đóng");
		btnng.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnng.setIcon(new ImageIcon(frmEditPhongKham.class.getResource("/images/6_3.png")));
		btnng.setFont(new Font("Cambria", Font.PLAIN, 14));
		toolBar.add(btnng);

		JLabel lblNewLabel = new JLabel("Mã phòng khám");
		lblNewLabel.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNewLabel.setBounds(20, 60, 106, 18);
		contentPane.add(lblNewLabel);

		JLabel lblTnPhngKhm = new JLabel("Tên phòng khám");
		lblTnPhngKhm.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblTnPhngKhm.setBounds(20, 110, 106, 18);
		contentPane.add(lblTnPhngKhm);

		JLabel lblThiGianM = new JLabel("Thời gian mở");
		lblThiGianM.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblThiGianM.setBounds(20, 160, 95, 18);
		contentPane.add(lblThiGianM);

		JLabel lblThiGianng = new JLabel("Thời gian đóng");
		lblThiGianng.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblThiGianng.setBounds(20, 210, 95, 18);
		contentPane.add(lblThiGianng);

		JLabel lblMKhoa = new JLabel("Tên khoa");
		lblMKhoa.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblMKhoa.setBounds(20, 260, 95, 18);
		contentPane.add(lblMKhoa);

		maPK = new JTextField();
		maPK.setEditable(false);
		maPK.setFont(new Font("Cambria", Font.PLAIN, 14));
		maPK.setBounds(156, 55, 258, 30);
		contentPane.add(maPK);
		maPK.setColumns(10);

		tenPK = new JTextField();
		tenPK.setFont(new Font("Cambria", Font.PLAIN, 14));
		tenPK.setColumns(10);
		tenPK.setBounds(156, 105, 258, 30);
		contentPane.add(tenPK);

		tenKhoa = new JComboBox(setItemJCC(m_objConnections));
		tenKhoa.setFont(new Font("Cambria", Font.PLAIN, 14));
		tenKhoa.setBounds(156, 255, 258, 30);
		contentPane.add(tenKhoa);

		oHour = new JComboBox();
		oHour.setModel(new DefaultComboBoxModel(new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08",
				"09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" }));
		oHour.setFont(new Font("Cambria", Font.PLAIN, 14));
		oHour.setBounds(156, 155, 52, 30);
		contentPane.add(oHour);

		oMinute = new JComboBox();
		oMinute.setModel(new DefaultComboBoxModel(new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08",
				"09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
				"26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42",
				"43", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59" }));
		oMinute.setFont(new Font("Cambria", Font.PLAIN, 14));
		oMinute.setBounds(278, 155, 52, 30);
		contentPane.add(oMinute);

		cHour = new JComboBox();
		cHour.setModel(new DefaultComboBoxModel(new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08",
				"09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" }));
		cHour.setFont(new Font("Cambria", Font.PLAIN, 14));
		cHour.setBounds(156, 205, 52, 30);
		contentPane.add(cHour);

		cMinute = new JComboBox();
		cMinute.setModel(new DefaultComboBoxModel(new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08",
				"09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
				"26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42",
				"43", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59" }));
		cMinute.setFont(new Font("Cambria", Font.PLAIN, 14));
		cMinute.setBounds(278, 205, 52, 30);
		contentPane.add(cMinute);

		JLabel lblNewLabel_1 = new JLabel("Giờ");
		lblNewLabel_1.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNewLabel_1.setBounds(218, 155, 46, 30);
		contentPane.add(lblNewLabel_1);

		JLabel lblPht = new JLabel("Phút");
		lblPht.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblPht.setBounds(340, 155, 46, 30);
		contentPane.add(lblPht);

		JLabel label_1 = new JLabel("Giờ");
		label_1.setFont(new Font("Cambria", Font.PLAIN, 14));
		label_1.setBounds(218, 205, 46, 30);
		contentPane.add(label_1);

		JLabel lblPht_1 = new JLabel("Phút");
		lblPht_1.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblPht_1.setBounds(340, 205, 46, 30);
		contentPane.add(lblPht_1);
		if (m_objPhongKhamEntity.isbFlag()) {
			btnNewButton.setText("Cập nhật");
			BindingControls(m_objPhongKhamEntity);
		} else
			btnNewButton.setText("Thêm mới");
		
	}

	public static Vector<String> setItemJCC(Connection objConnection) {
		CallableStatement objCmst = null;
		ResultSet objRs = null;

		Vector<String> item = new Vector<String>();
		try {
			objCmst = objConnection.prepareCall("{call SP_GET_TENKHOA()}");

			objRs = objCmst.executeQuery();

			while (objRs.next()) {
				item.addElement(objRs.getString("TenKhoa"));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.getMessage();
		}
		return item;
	}

	public void BindingControls(PhongKhamEntity objPhongKhamEntity) {
		maPK.setText(objPhongKhamEntity.getMaPhongKham());
		tenPK.setText(objPhongKhamEntity.getTenPhongKham());
		tenKhoa.setSelectedIndex(objPhongKhamEntity.getMaKhoa());
		String opentime = objPhongKhamEntity.getOpenTime();
		String closetime = objPhongKhamEntity.getCloseTime();
		oHour.setSelectedItem(opentime.substring(0, 2));
		oMinute.setSelectedItem(opentime.substring(3, 5));
		cHour.setSelectedItem(closetime.substring(0, 2));
		cMinute.setSelectedItem(closetime.substring(3, 5));
	}
}
